﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Web.UI.WebControls;

namespace System.Web.UI
{
    public class ZMPageSNDetail<T, SModel, NModel> : ZMPageEdit<SModel>
        where T : SNModel<SModel, NModel>, new()
        where SModel : BaseModel, new()
        where NModel : BaseModel, new()
    {
        protected virtual T DataModel { get; set; }
        protected GridView baseNGrid = null;

        protected virtual GridView BaseNGrid
        {
            get
            {
                RegNGrid();
                return baseNGrid;
            }
        }
        protected virtual void RegNGrid()
        {
            if (baseNGrid == null)
            {
                if (Master != null)
                {
                    switch (ZConfig.TemplateLayer)
                    {
                        case 1:
                            baseNGrid = (GridView)Page.Master.Master.FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("gvInfos");
                            break;
                        case 2:
                            baseNGrid = (GridView)Page.Master.Master.Master.FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("gvInfos");
                            break;
                        default:
                            baseNGrid = (GridView)Page.Master.Master.FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("gvInfos");
                            break;
                    }
                }
                else
                    baseNGrid = Page.FindControl("gvInfo") as GridView;
            }
        }       

        protected virtual string SubGridDefaultFilter
        {
            get
            {
                return "1=1";
            }
        }

        protected override void SetControlsProperty()
        {
            base.SetControlsProperty();
            SetGridViewProperty(BaseNGrid);
            NModel t = new NModel();
            string primaryKey = t.GetPrimaryKey();
            if (!string.IsNullOrEmpty(primaryKey))
                BaseNGrid.DataKeyNames = new string[] { primaryKey };
        }

        protected override void InitModel()
        {
            base.InitModel();
            DataModel = new T();
            DataModel.Model = Model;
            if (!IsNewModel)
            {
                BaseBLL<NModel> bll = new BaseBLL<NModel>();
                DataModel.NModles = bll.GetList("", DataModel.NRelationFieldName + "='" + CModel.GetProValue(DataModel.Model, DataModel.Model.GetPrimaryKey()) + "' AND " + SubGridDefaultFilter, "", null);
                JavaScriptSerializer js = new JavaScriptSerializer();
                DataModel.NSubListJsonData = js.Serialize(DataModel.NModles).Replace("\"null\"", "\"\"");
            }
        }

        protected override void OnBind()
        {
            base.OnBind();
            if (DataModel != null && DataModel.NModles != null && BaseNGrid != null)
            {
                OnGridDataBinding();
                BaseNGrid.DataSource = DataModel.NModles;
                BaseNGrid.DataBind();
            }
        }


        protected override void RegControlsEvent()
        {
            base.RegControlsEvent();
            if (BaseNGrid != null)
            {
                BaseNGrid.RowDataBound += new GridViewRowEventHandler(OnMainGridRowDataBound);
                BaseNGrid.RowCommand += new GridViewCommandEventHandler(OnMainGridRowCommand);
                BaseNGrid.Sorting += new GridViewSortEventHandler(OnMainGridSorting);
            }
        }
        protected virtual void OnMainGridSorting(object sender, GridViewSortEventArgs e)
        {

        }
        protected virtual void OnMainGridRowCommand(object sender, GridViewCommandEventArgs e)
        {

        }
        protected virtual void OnMainGridRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "currentcolor = this.style.backgroundColor;this.style.backgroundColor='#afd6f5';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor = currentcolor;");
            }
        }


        protected virtual void OnGridDataBinding()
        {
            if (ZSetup.IFUseLanguage)
            {
                foreach (DataControlField field in baseNGrid.Columns)
                {
                    field.HeaderText = ZLanguages.GetLanguages(field.HeaderText);
                    if (field is CommandField)
                    {
                        CommandField cfield = field as CommandField;
                        cfield.CancelText = ZLanguages.GetLanguages("App_Cancel");
                        cfield.EditText = ZLanguages.GetLanguages("App_Edit");
                        cfield.UpdateText = ZLanguages.GetLanguages("App_Update");
                    }
                    if (field is HyperLinkField)
                    {
                        HyperLinkField hField = field as HyperLinkField;
                        if (hField.Text == "App_View" || hField.Text == "App_Modify")
                        {
                            hField.Text = ZLanguages.GetLanguages(hField.Text);
                        }
                    }
                }
            }
        }
    }
}
